home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group00b.txt / 000135_icon-group-sender_Wed Nov 1 13:29:23 2000.msg < prev    next >
Internet Message Format  |  2001-01-03  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.11.1/8.11.1) id eA1KQNk17061
  4.     for icon-group-addresses; Wed, 1 Nov 2000 13:26:23 -0700 (MST)
  5. Message-Id: <200011012026.eA1KQNk17061@baskerville.CS.Arizona.EDU>
  6. Date: Wed, 01 Nov 2000 11:30:53 -0600
  7. From: "Charles Hethcoat" <CHETHCOA@oss.oceaneering.com>
  8. To: <icon-group@cs.arizona.edu>
  9. Subject: favorites 2
  10. Content-Disposition: inline
  11. X-Guinevere: 1.0.13 ; Oceaneering Int'l
  12. X-MIME-Autoconverted: from quoted-printable to 8bit by baskerville.CS.Arizona.EDU id eA1HUH311489
  13. Errors-To: icon-group-errors@cs.arizona.edu
  14. Status: RO
  15. Content-Length: 1873
  16.  
  17. In the spirit of the times, and in view of the apparent arrival of a lot of newcomers to Icon, let me also submit a fave of mine.
  18.  
  19. I write a lot of rather simple batch-oriented stuff (read a line, operate on the line, write something, repeat).  A comment convention is just as useful with such data as with any programming language.  Your data files can now be self-documenting without interfering with the program that needs to understand their formatting.
  20.  
  21. Here is a routine that implements a simple comment convention just like Icon's, so that every program I write is consistent in that respect.  It is simple stuff, but I have come to rely on its presence in my library.  Here it is, with a simple main() to drive it, and some test data for it.  To test it, just enter the command
  22.  
  23. nocomm <nocomm.in >nocomm.out
  24.  
  25. THE PROGRAM (save as nocomm.icn):
  26.  
  27. procedure main()
  28.     while line := nocomm(&input) do
  29.         write(line)
  30.     exit(0)
  31. end
  32.  
  33. # nocomm - implement a basic comment convention
  34. procedure nocomm(f)
  35.     static line
  36.  
  37.     while line := read(f) do {
  38.         line ? {
  39.             ws()
  40.             if ="#" | pos(0) then next
  41.             return trim(tab(upto('#') | 0))
  42.         }
  43.     }
  44.     fail
  45. end
  46.  
  47. # ws - skip white space (blanks and tabs)
  48. procedure ws()
  49.      tab(many(' \t'))
  50. end
  51.  
  52. INPUT (save as nocomm.in):
  53.  
  54. This file shows how nocomm() implements a comment convention.
  55. The following is a blank line.
  56.  
  57. It should be gone.  nocomm() removes blank lines.
  58. This line contains a comment.  # It's too darn hot.
  59. The next line is a comment.
  60. # Slooooowly I turned, step by step.
  61. The next line is also a comment.
  62.           # hello.
  63. This is the last line.
  64.  
  65. OUTPUT (nocomm.out):
  66.  
  67. This file shows how nocomm() implements a comment convention.
  68. The following is a blank line.
  69. It should be gone.  nocomm() removes blank lines.
  70. This line contains a comment.
  71. The next line is a comment.
  72. The next line is also a comment.
  73. This is the last line.
  74.  
  75.